#include "server.hpp"

// void HandleDisconnect(Channel* channel)
// {
//     DBG_LOG("%s: %d","close",channel->GetFd());
//     channel->RemoveListen();
//     close(channel->GetFd());
//     delete channel;
// }

// void HandleRead(Channel* channel)
// {
//     char buffer[32] = {0};
//     int n = recv(channel->GetFd(),buffer,31,0);
//     //一般不会立马断开连接，服务端可能还需要向客户端发送数据
//     if(n <= 0)
//     {
//         HandleDisconnect(channel);
//         return;
//     }
//     DBG_LOG("%s",buffer);

//     //获取到了数据应该放入我们的buffer模块中，但是这里为了测试，就先不加。
//     channel->EnableWriteListen();
// }

// void HandleWrite(Channel* channel)
// {
//     std::string str = "你好啊!!!!!";
//     int n = send(channel->GetFd(),str.c_str(),str.size(),0);

//     //如果写入出错，直接关闭连接
//     if(n < 0)
//     {
//         HandleDisconnect(channel);
//         return;
//     }

//     //写完就关闭，不然就会一直写事件触发
//     channel->DisableWriteListen();
// }

// void HandleError(Channel* channel)
// {
//     HandleDisconnect(channel);
// }


// void HandleAny(Channel* channel,EventLoop* loop,uint64_t timerid)
// {
//     DBG_LOG("这是一个事件");
//     loop->TimerTaskRefresh(timerid);
// }



uint64_t con_id = 0;
std::unordered_map<uint64_t,SharedptrCon> conns_;

void OnServerDisconnect(const SharedptrCon& con)
{
    conns_.erase(con->GetId());
}

void OnDisConnect(const SharedptrCon& con)
{
    DBG_LOG("RELEASE CONNECTION: %p",con.get());
}

void OnConnect(const SharedptrCon& con)
{
    DBG_LOG("CONNECTION CREATE SUCCESS!!");
}

void OnMessage(const SharedptrCon& con,Buffer* buf)
{
    char buffer[65535];
    buf->ReadAndPop(buffer,buf->ReadableSize());
    DBG_LOG("%s",buffer);
    std::string ret = "这是一个处理结果";
    con->Send(ret.c_str(),ret.size());
    con->ShutDown();
}

// void Acceptor(Channel* listen_channel,EventLoop* loop)
// {
//     int newfd = accept(listen_channel->GetFd(),nullptr,nullptr);
//     if(newfd < 0)
//     {
//         ERR_LOG("SOCKET ACCEPT ERROR!!!");
//         return;
//     }
//     con_id++;
//     SharedptrCon con(new Connection(loop,con_id,newfd));


//     con->SetConnectCallback(std::bind(OnConnect,std::placeholders::_1));
//     con->SetMessagetCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
//     con->SetDisconnectCallback(std::bind(OnDisConnect,std::placeholders::_1));
//     con->SetServerDisconnectCallback(std::bind(OnServerDisconnect,std::placeholders::_1));

//     con->Established();
//     con->EableInactiveRelease(10);
//     conns_.insert(std::make_pair(con_id,con));
// }


EventLoop main_loop;
//自动分配从属线程得eventloop给每个connection
LoopThreadPool* thread_pool;


void HandleAccept(int newfd)
{
    con_id++;
    SharedptrCon con(new Connection(thread_pool->GetLoop(),con_id,newfd));

    con->SetConnectCallback(std::bind(OnConnect,std::placeholders::_1));
    con->SetMessagetCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
    con->SetDisconnectCallback(std::bind(OnDisConnect,std::placeholders::_1));
    con->SetServerDisconnectCallback(std::bind(OnServerDisconnect,std::placeholders::_1));

    con->Established();
    con->EableInactiveRelease(10);
    conns_.insert(std::make_pair(con_id,con));
    
    DBG_LOG("NEW CONNETING SUCCESS---------------------------");
}


int main()
{
    srand(time(nullptr));
    thread_pool = new LoopThreadPool(&main_loop);
    thread_pool->SetThreadCount(2);
    thread_pool->Create();
    // Socket listen_socket;
    // // 因为我们服务端的listen套接字创建好默认是阻塞的。
    // // 现在加一个参数，如果想要设置非阻塞，就加一个true
    // listen_socket.CreateServer(8888);

    // Channel* listen_channel = new Channel(&loop,listen_socket.GetFd());
    // listen_channel->SetReadCallback(std::bind(Acceptor,listen_channel,&loop));//设置读回调
    // listen_channel->EnableReadListen();//启动可读事件监控
    Acceptor listen_accept(&main_loop,8888);
    listen_accept.SetAcceptCallback(std::bind(HandleAccept,std::placeholders::_1));
    listen_accept.EnableRead();
    main_loop.Start();
    return 0;
}